package com.cindata

import org.apache.poi.hssf.usermodel.HSSFCell

import org.apache.poi.hssf.usermodel.HSSFRichTextString
import org.apache.poi.hssf.usermodel.HSSFRow
import org.apache.poi.hssf.usermodel.HSSFSheet
import org.apache.poi.hssf.usermodel.HSSFWorkbook
import org.apache.poi.ss.formula.functions.T
import java.text.SimpleDateFormat

class ExcelExportService
{

    //报表数据导出下载
    def exportExcel(String title, String[] headers, List<Map<String, String>> dataset, String[] params)
    {
        // 声明一个工作薄
        HSSFWorkbook workbook = new HSSFWorkbook();
        // 生成一个表格
        HSSFSheet sheet = workbook.createSheet(title);
        // 设置表格默认列宽度为15个字节
        sheet.setDefaultColumnWidth((short) 15);
        // 写表格头
        HSSFRow row = sheet.createRow(0);
        for (
            short i = 0;
                i < headers.length;
                i++)
        {
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
        }

        //写表格数据
        def i = 1;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (
            Map<String, T> map :
                dataset)
        {
            HSSFRow data = sheet.createRow(i);
            int j = 0;
            for (
                String s :
                    params)
            {
                HSSFCell cell = data.createCell(j);
                if (map.get(s) != null && map.get(s) != "null")
                {
                    cell.setCellValue(map.get(s));
                }
                else
                {
                    cell.setCellValue("");
                }
                j++;
            }
            i++;
        }
        return workbook;
    }

    def exportExcelData(OutputStream out, List rowMapper, List exportData)
    {
        //写入文件头部
        StringBuffer head = new StringBuffer()
        for (
            Iterator pi = rowMapper.iterator();
                pi.hasNext();)
        {
            def property = pi.next()
            head.append(property.toString())
            if (pi.hasNext())
            {
                head.append(",")
            }
        }
        head.append("\n")
        out.write(head.toString().getBytes("GBK"))
        out.flush()
        //写入文件内容
        def buf = new StringBuffer()
        for (
            Iterator iterator = exportData.iterator();
                iterator.hasNext();)
        {
            def list = iterator.next()
            for (
                Iterator pi = list.iterator();
                    pi.hasNext();)
            {
                def property = pi.next()
                buf.append(property.toString())
                if (pi.hasNext())
                {
                    buf.append(",")
                }
            }
            if (iterator.hasNext())
            {
                buf.append("\n")
            }
        }
        out.write(buf.toString().getBytes("GBK"))
        out.flush()
        out.close()
        return null
    }
}